// 伪继承 baseController
app.controller('brandController', function ($scope, $controller, brandService) {
    $controller('baseController', {$scope: $scope});//继承

    // 查询所有
    $scope.findAll = function () {
        brandService.findAll().success(
            function (response) {
                $scope.list = response;
            }
        );
    }

    //分页查询
    $scope.findPage = function (page, size) {
        brandService.findPage(page, size).success(function (response) {
            $scope.list = response.rows;//当前页面数据
            $scope.paginationConf.totalItems = response.total;//更新总记录数
        });
    }

    //按照Id查询
    $scope.selectOne = function (id) {
        brandService.selectOne(id).success(function (response) {
            //填充双向绑定的model
            $scope.entity = response;
        });
    }


    //按照id数组删除品牌
    $scope.del = function () {
        brandService.del($scope.selectionId).success(function (response) {
            if (response.success) {
                //删除成功，刷新数据
                $scope.reloadList();
                $scope.selectionId = [];
            } else {
                //删除失败，提示
                alert(response.message);
            }
        });
    }


    //保存（包括新建、更新）
    $scope.save = function () {

        // 定义服务层对象，用于区别是新增、更新
        var serviceObject;
        if ($scope.entity.id != null) {
            // 有ID，为更新
            serviceObject = brandService.update($scope.entity);
        } else {
            serviceObject = brandService.add($scope.entity);
        }
        serviceObject.success(function (response) {
            if (response.success) {
                //保存成功，刷新数据
                $scope.reloadList();
            } else {
                //保存失败，提示
                alert(response.message);
            }
        });
    }

    $scope.searchEntity = {} // 定义搜索对象
    // 搜索
    $scope.search = function (page, rows) {
        brandService.search(page, rows, $scope.searchEntity).success(
            function (response) {
                $scope.list = response.rows;//当前页面数据
                $scope.paginationConf.totalItems = response.total;//更新总记录数
            }
        );
    }
});